package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.QuestionDto;
import com.jyy.prefabricated.entity.Option;
import com.jyy.prefabricated.entity.Question;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.OptionMapper;
import com.jyy.prefabricated.mapper.QuestionDetailsMapper;
import com.jyy.prefabricated.mapper.QuestionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Transactional
@Service
public class QuestionService {

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private OptionMapper optionMapper;

    @Resource
    private QuestionDetailsMapper questionDetailsMapper;

    /**
     * 新增
     *
     * @param questionDto
     * @return
     */
    public int insert(QuestionDto questionDto) {
        if (questionDto.getQuestionType() != null) {
            Question question = questionDto.toQuestion();
            if (question.getMaterialQuestionId() != null) {
                Integer sort = questionMapper.selectMaxSort(question.getMaterialQuestionId());
                if (sort == null) {
                    question.setSort(0);
                } else {
                    question.setSort(sort + 1);
                }
            }
            questionMapper.insert(question);
            /*if(questionDto.getQuestionType() == 0 || questionDto.getQuestionType() == 1){
                saveQuestionId2Option(questionDto.getOptions(), question.getId());
            }else if(questionDto.getQuestionType() == 5){
                if(questionDto.getQuestionDto() != null){
                    for (QuestionDto questionDtos:questionDto.getQuestionDto()) {
                        if(questionDtos.getQuestionType() != null){
                            Question questions = questionDtos.toQuestion();
                            questions.setMaterialQuestionId(question.getId());
                            questionMapper.updateByPrimaryKey(questions);
                            if(questionDtos.getQuestionType() == 0 || questionDtos.getQuestionType() == 1){
                                saveQuestionId2Option(questionDtos.getOptions(), questions.getId());
                            }
                        }
                    }
                }
            }*/
            return question.getId();
        }
        return questionDto.getId();
    }

    /**
     * 复制转移题目
     *
     * @param questionId 题目ID type 1复制 2转移
     * @return
     */
    public int copyTransfer(Integer questionBankId, Integer questionId, Integer type) {
        Question question = selectById(questionId);
        if (checkQuestion(questionBankId, question.getName())) {
            throw new CustomException("题库已存在相同题目");
        }
        if (question.getQuestionType() != null) {
            question.setId(null);
            question.setQuestionBankId(questionBankId);
            questionMapper.insert(question);
            if (question.getQuestionType() == 0 || question.getQuestionType() == 1) {
                saveQuestionId2Option(question.getOptions(), question.getId());
            } else if (question.getQuestionType() == 5) {
                if (question.getQuestions() != null) {
                    for (Question questions : question.getQuestions()) {
                        if (questions.getQuestionType() != null) {
                            questions.setId(null);
                            questions.setMaterialQuestionId(question.getId());
                            questionMapper.insert(questions);
                            if (questions.getQuestionType() == 0 || questions.getQuestionType() == 1) {
                                saveQuestionId2Option(questions.getOptions(), questions.getId());
                            }
                        }
                    }
                }
            }
            if (type == 2) {
                delete(questionId);
            }
        }

        return questionBankId;
    }

    /**
     * 校验重名题目
     *
     * @param questionBankId 题库id name 题目
     * @return
     */
    public Boolean checkQuestion(Integer questionBankId, String name) {
        List<Question> questions = questionMapper.selectByQuestionBankIdAndName(questionBankId, name);
        for (Question question:questions) {
            if (question.getQuestionType() == 0 || question.getQuestionType() == 1) {
                optionMapper.selectByQuestionId(question.getId());
            }
        }
        return questions.size() > 0;
    }
    public Boolean checkQuestionOption(Integer questionBankId, String name,List<Option> options) {
        List<Question> questions = questionMapper.selectByQuestionBankIdAndName(questionBankId, name);
        int optionNot = 0;
        for (Question question:questions) {
            if (question.getQuestionType() == 0 || question.getQuestionType() == 1) {
                if (options != null && options.size() > 0) {
                    List<Option> QuestionOption = optionMapper.selectByQuestionId(question.getId());
                    if(options.size() == QuestionOption.size() ){
                        for (int i = 0; i<QuestionOption.size();i++) {
                            if(!QuestionOption.get(i).getContent().equals(options.get(i).getContent())){
                                optionNot ++;
                                break;
                            }
                        }
                    }else{
                        optionNot ++;
                    }
                }

            }
        }
        if(optionNot == questions.size()){
            return false;
        }
        return questions.size() > 0;
    }
    /**
     * 批量新增
     *
     * @param questions
     * @return
     */
    public int batchInsert(List<Question> questions) {
        int row = 0;
        for (Question question : questions) {
            if (!checkQuestionOption(question.getQuestionBankId(), question.getName(),question.getOptions())) {
                question.setDeleted(true);
                questionMapper.insert(question);
                if (question.getQuestionType() == 0 || question.getQuestionType() == 1) {
                    if (question.getOptions().size() > 0) {
                        for (Option option : question.getOptions()) {
                            option.setQuestionId(question.getId());
                        }
                    }
                    optionMapper.batchInsert(question.getOptions());
                }
                row++;
            }
        }
        return row;
    }

    /**
     * 将题目id插入到选项中
     *
     * @param options
     * @param id
     */
    private void saveQuestionId2Option(List<Option> options, Integer id) {
        if (options.size() > 0) {
            for (Option option : options) {
                option.setQuestionId(id);
            }
            optionMapper.updateBatch(options);
        }
    }

    /**
     * 更新
     *
     * @param questionDto
     * @return
     */
    public int update(QuestionDto questionDto) {
        if (questionDto.getId() == null) {
            throw new CustomException("参数id不能为空");
        }
        if (questionDto.getQuestionType() != null) {
            Question question = questionDto.toQuestion();
            questionMapper.updateByPrimaryKeySelective(question);
            if (questionDto.getQuestionType() == 0 || questionDto.getQuestionType() == 1) {
                saveQuestionId2Option(questionDto.getOptions(), question.getId());
            } else if (questionDto.getQuestionType() == 5) {
                if (questionDto.getQuestionDto() != null) {
                    for (QuestionDto questionDtos : questionDto.getQuestionDto()) {
                        if (questionDtos.getQuestionType() != null) {
                            Question questions = questionDtos.toQuestion();
                            questionMapper.updateByPrimaryKeySelective(questions);
                            if (questionDto.getQuestionType() == 0 || questionDto.getQuestionType() == 1) {
                                saveQuestionId2Option(questionDtos.getOptions(), questions.getId());
                            }
                        }
                    }
                }
            }
            return question.getId();
        }
        return questionDto.getId();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        Question question = new Question();
        question.setId(id);
        question.setDeleted(false);
        optionMapper.deleteByQuestionId(id);
        questionMapper.deleteByMaterialQuestionId(id);
        return questionMapper.updateByPrimaryKeySelective(question);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Question selectById(Integer id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        if (question.getQuestionType() == 5) {
            List<Question> questions = questionMapper.selectByParams(null, null, null, null, null, null, question.getId(), null ,null);
            for (Question qt : questions) {
                if (qt.getQuestionType() == 0 || qt.getQuestionType() == 1) {
                    qt.setOptions(optionMapper.selectByQuestionId(qt.getId()));
                }
            }
            question.setQuestions(questions);
        }
        return question;
    }

    /**
     * 根据参数查询列表
     *
     * @param questionBankId 题库id
     * @param questionType   题目类型
     * @param stemType       题干类型
     * @param difficulty     题目难度
     * @return
     */
    public List<Question> selectByParams(Integer questionBankId, Integer questionType, Integer stemType, Integer category, Integer type, Integer difficulty, Integer materialQuestionId, Integer questionState, Integer trainingOrganizationId) {
        return questionMapper.selectByParams(questionBankId, questionType, stemType, category, type, difficulty, materialQuestionId, questionState, trainingOrganizationId);
    }

    /**
     * 题目排序
     *
     * @param questionId 交换排序id1 questionIdTwo 交换排序id2
     * @return
     */
    public int questionsSort(Integer questionId, Integer questionIdTwo) {
        Question question = questionMapper.selectByPrimaryKey(questionId);
        Question questionTwo = questionMapper.selectByPrimaryKey(questionIdTwo);
        Integer sort = question.getSort();
        Integer sortTwo = questionTwo.getSort();
        question.setSort(sortTwo);
        questionTwo.setSort(sort);
        questionMapper.updateByPrimaryKeySelective(question);
        return questionMapper.updateByPrimaryKeySelective(questionTwo);
    }
}
